package cc.lj.internal.payment.application;

import cc.lj.internal.api.coupons.api.ticket.CouponsSettlementFeignService;
import cc.lj.internal.api.coupons.api.ticket.TicketBatchFeignService;
import cc.lj.internal.api.coupons.api.ticket.TicketPayRecordFeignService;
import cc.lj.internal.api.coupons.dto.CouponsSettlementApiDTO;
import cc.lj.internal.api.coupons.dto.TicketBatchApiDTO;
import cc.lj.internal.api.coupons.dto.TicketPayRecordApiDTO;
import cc.lj.internal.api.coupons.dto.command.CouponsSettlementApiCommand;
import cc.lj.internal.api.coupons.dto.command.TicketPayRecordApiUpdateCommand;
import cc.lj.internal.api.coupons.dto.querty.TicketPayRecordApiQuery;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.http.HttpResult;
import cc.lj.internal.payment.infrastructure.exception.PaymentExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 应用层通用调用外部服务类
 * @author liangxifeng
 * @date 2022-01-12
 */
@Slf4j
@Service("feignAppService")
public class FeignAppService {
    //电子券模块,合同收券表 feign api
    @Resource(type = TicketPayRecordFeignService.class)
    private TicketPayRecordFeignService ticketPayRecordFeignService;

    //电子券模块,电子券批次表 feign api
    @Resource(type = TicketBatchFeignService.class)
    private TicketBatchFeignService ticketBatchFeignService;

    //电子券模块,电子券结算数据获取 feign api
    @Resource(type = CouponsSettlementFeignService.class)
    private CouponsSettlementFeignService settlementFeignService;

    /**
     * 查询合同收款表list,无分页
     * @param apiQuery 查询query
     * @return 合同收券表List
     */
    public List<TicketPayRecordApiDTO> getList(TicketPayRecordApiQuery apiQuery){
        HttpResult<List<TicketPayRecordApiDTO>> listHttpResult = ticketPayRecordFeignService.getList(apiQuery);
        if(listHttpResult == null || listHttpResult.getCode() != 1) {
            log.info("去电子券服务查询合同收款表getList接口时出现错误，错误返回:"+listHttpResult);
            throw new ServiceException(PaymentExceptionEnum.FEIGN_GET_TICKET_PAY_RECORD_LIST_FAILED);
        }
        return listHttpResult.getData();
    }

    /**
     *
     * 通过主键批量查询电子券批次
     * @param batchIds 电子券批次表主键list
     * @return
     */
    public List<TicketBatchApiDTO> getTicketBatchByIds(List<Integer> batchIds) {
        HttpResult<List<TicketBatchApiDTO>> listHttpResult = ticketBatchFeignService.getListByIds(batchIds);
        if(listHttpResult == null || listHttpResult.getCode() != 1 || listHttpResult.getData().size() == 0) {
            log.info("去电子券服务查询电子券批次表,通过主键批量查询接口时出现错误，错误返回:"+listHttpResult);
            throw new ServiceException(PaymentExceptionEnum.FEIGN_GET_TICKET_BATCH_BYIDS_FAILED);
        }
        return listHttpResult.getData();
    }

    /**
     * 去电子券模块，查询电子券分摊结算需要的数据
     * @param apiCommand
     * @return
     */
    public CouponsSettlementApiDTO getCouponsSettlementData(CouponsSettlementApiCommand apiCommand) {
        HttpResult<CouponsSettlementApiDTO> result = settlementFeignService.getSettlementData(apiCommand);
        if(result == null || result.getCode() != 1 ) {
            log.info("去电子券服务查询电子券结算分摊数据出现异常，错误返回:"+result);
            throw new ServiceException(PaymentExceptionEnum.FEIGN_GET_COUPONS_SETTLEMENT_DATA_FAILED);
        }
        return result.getData();
    }

    /**
     * 去电子券模块，通过主键修改合同收券表数据
     * @param ticketPayRecordApiUpdateCommand
     * @return 修改后的影响行数
     */
    public Integer updateTicketPayRecord(TicketPayRecordApiUpdateCommand ticketPayRecordApiUpdateCommand) {
        HttpResult<Integer> updateRes = ticketPayRecordFeignService.updateById(ticketPayRecordApiUpdateCommand);
        if(updateRes == null || updateRes.getCode() != 1) {
            log.info("去电子券服务通过主键修改合同收券表数据出现异常，错误返回:"+updateRes);
            throw new ServiceException(PaymentExceptionEnum.FEIGN_UPDATE_TICKET_PAY_RECORD_FAILED);
        }
        return updateRes.getData();
    }
}
